//
//  XQUserSettingController.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/21.
//

#import "XQUserSettingController.h"
#import "XQTopCardView.h"
#import "XQBaseTableListArrowView.h"
#import "XQAccountCancelController.h"
#import "XQAttentionPopView.h"
#import "XQPopupAnimView.h"
#import "XQUserController.h"

@interface XQUserSettingController ()
@property (nonatomic,strong) XQTopCardView *cardView;
@property (nonatomic,copy) NSArray *dataSource;
@property (nonatomic,strong) XQBaseTableListArrowView *tableView;
@end

@implementation XQUserSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeNav];
    [self makeUI];
    [self makeConstraints];
}

- (void)makeNav {
    [self.navigationBar setTitle:@"Settings"];
}

- (void)makeUI {
    self.view.backgroundColor = kCardColor;
    [self.view addSubview:self.cardView];
    [self.view addSubview:self.tableView];
}

- (void)makeConstraints {
    [self.cardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([UIScreen mainScreen].topHeight + 8);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cardView.mas_bottom).offset(20);
        make.left.right.equalTo(@0);
    }];
}

#pragma mark - private

- (void)cellDidClickWithName:(NSString *)name {
    if ([name isEqualToString:@"Account cancelation"]) {
        XQAccountCancelController *vc = [XQAccountCancelController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([name isEqualToString:@"Exit"]) {
        [self exitClick];
    }
}

- (void)exitClick {
    
    XQAttentionPopView *popView = [[XQAttentionPopView alloc] init];
    popView.title = @"Attention";
    popView.content = @"You can get a loan in only 1-3 minutesAre you sure you are going to leave?";
    
    XQPopupAnimView *anim = [[XQPopupAnimView alloc] initWithAnimationType:XQPopupAnimationCenter customView:popView];
    anim.needCover = YES;
    anim.makeCustomConstraints = ^(XQPopupAnimView *animView, UIView *customView) {
        [customView mas_remakeConstraints:^(MASConstraintMaker *make) {
            (void)make.center;
        }];
    };
    
    [anim showInView:self.view completion:^{}];
    
    @weakify(anim)
    popView.confimHandle = ^{
        @strongify(anim);
        
        [self loginOut];
        [anim hideAndCompletion:^{}];
    };
    popView.closeHandle = ^{
        @strongify(anim);
        [anim hideAndCompletion:^{}];
    };
}

- (void)loginOut {
    [XQHUD showLoadingInView:self.view];
    @weakify(self)
    [XQAFNetworking getWithUrl:[XQNetManager getAbsolutePath:@"/direct/players"] pars:@{} callback:^(_Bool success, id  _Nonnull responseObject, NSError * _Nonnull error) {
        @strongify(self);
        [XQHUD hideLoadindInView:self.view];
        if ([responseObject[@"concept"] integerValue] == 0) {
            [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"continuous"];
            [[XQNetManager sharedInstance].globalDic setValue:@"" forKey:@"continuous"];
            
            [self.navigationController popViewControllerAnimated:YES];

            [XQRouter jumpToHome];
        }
        
    }];
}

#pragma mark - lazy

- (XQTopCardView *)cardView {
    if (!_cardView) {
        _cardView = [[XQTopCardView alloc] init];
        [_cardView setNick:[XQToolManager manager].userName];
    }
    return _cardView;
}

- (XQBaseTableListArrowView *)tableView {
    if (!_tableView) {
        _tableView = [XQBaseTableListArrowView new];
        _tableView.dataSource = self.dataSource;
        @weakify(self);
        _tableView.cellClickHandle = ^(NSString * _Nonnull title) {
            @strongify(self);
            [self cellDidClickWithName:title];
        };
    }
    return _tableView;
}

- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource = @[
            @{
                @"won" :@"Account cancelation"
            },
            @{
                @"won" :@"Exit"
            }
        ];
    }
    return _dataSource;
}

@end
